package com.base.httplisten;
import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;


public class WebServerListen {

	//监听套接字
	private int port;
	public WebServerListen(int port) {
			this.port = port;
			// TODO Auto-generated constructor stub
		}
	//监听别的
	public void run() throws Exception {
		 ServerBootstrap bootstrap = new ServerBootstrap();
	        EventLoopGroup boss = new NioEventLoopGroup();
	        EventLoopGroup work = new NioEventLoopGroup();
	        bootstrap.group(boss,work)
	                .channel(NioServerSocketChannel.class)
	    	        .childHandler( new ChannelInitializer<SocketChannel>() {
	    	            @Override
	    	            protected void initChannel(SocketChannel ch) throws Exception {
	    	            	
	    	                ChannelPipeline pipeline = ch.pipeline();
	    	                pipeline.addLast(new HttpServerCodec());// http 编解码
	    	                pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024)); // http 消息聚合器                                                                     512*1024为接收的最大contentlength
	    	                pipeline.addLast(new WebServerListenHandler());// 请求处理器
	    	            }
	    	        });

	        ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync();
	        System.out.println(" server start up on port : " + port);
	        f.channel().closeFuture().sync();
	}
}
